package com.hongxinguoji.manager.admin.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hongxinguoji.manager.admin.mapper.HxAuthFunctionMapper;
import com.hongxinguoji.manager.admin.mapper.HxUserMapper;
import com.hongxinguoji.manager.admin.mapper.HxUserRoleMapper;
import com.hongxinguoji.manager.admin.pojo.HxAuthFunction;
import com.hongxinguoji.manager.admin.pojo.HxAuthFunctionExample;
import com.hongxinguoji.manager.admin.pojo.HxUser;
import com.hongxinguoji.manager.admin.pojo.HxUserExample;
import com.hongxinguoji.manager.admin.pojo.HxUserRoleExample;
import com.hongxinguoji.manager.admin.pojo.HxUserRoleKey;
import com.hongxinguoji.manager.admin.service.IUserService;
import com.hongxinguoji.manager.common.util.MD5Utils;

@Service
public class UserServiceImpl implements IUserService {

	@Autowired
	private HxUserMapper userMapper;

	@Autowired
	private HxAuthFunctionMapper functionMapper;

	@Autowired
	private HxUserRoleMapper userRoleMapper;

	@Override
	public List<HxUser> getUserList() {
		
		HxUserExample example = new HxUserExample();
		List<HxUser> list = userMapper.selectByExample(example);
		return list;
	}

	@Override
	public void addUser(HxUser user, String[] roleids) {
		// 密码MD5加密操作
		String password = user.getPassword();
		password = MD5Utils.md5(password);
		user.setPassword(password);

		String id = user.getId();

		Date date = new Date();
		user.setUpdatetime(date);
		user.setCreatetime(date);

		// 默认状态可用
		user.setStatus(1);

		// 插入数据
		userMapper.insert(user);

		if (StringUtils.isNoneBlank(roleids)) {

			// 维护外键关系
			for (String roleId : roleids) {
				HxUserRoleKey userRoleKey = new HxUserRoleKey();
				userRoleKey.setRoleId(roleId);
				userRoleKey.setUserId(id);

				userRoleMapper.insert(userRoleKey);
			}

		}

	}

	@Override
	public List<HxAuthFunction> getUserMenuList(HxUser user) {
		// 获取用户名
		String username = user.getUsername();
		if ("admin".equals(username)) {
			HxAuthFunctionExample example = new HxAuthFunctionExample();
			// 过滤不显示的菜单
			example.createCriteria().andGeneratemenuEqualTo("1");
			// 对菜单排序
			example.setOrderByClause("zindex");
			// 获取全部树形菜单
			List<HxAuthFunction> list = functionMapper.selectByExample(example);
			return list;
		} else {
			String id = user.getId();
			// 根据ID查询权限菜单
			List<HxAuthFunction> list = functionMapper.getUserAuthByUserId(id);
			return list;
		}
	}

	@Override
	public void changePwd(String userId, String password) {
		// 去掉密码的两端空格
		password = password.trim();
		// 密码加密
		password = MD5Utils.md5(password);

		HxUser user = userMapper.selectByPrimaryKey(userId);
		Date date = new Date();
		user.setUpdatetime(date);
		user.setPassword(password);

		// 更新用户密码操作
		userMapper.updateByPrimaryKey(user);

		Subject subject = SecurityUtils.getSubject();
		if (subject.isAuthenticated()) {
			// session 会销毁，在SessionListener监听session销毁，清理权限缓存
			subject.logout();
		}
	}

	@Override
	public void updateUser(HxUser user, String[] roleIds) {

		String id = user.getId();

		// 通过id查询用户信息
		HxUser hxUser = userMapper.selectByPrimaryKey(id);

		// 更新数据
		hxUser.setCompany(user.getCompany());
		hxUser.setDepartment(user.getDepartment());
		hxUser.setEmail(user.getEmail());
		hxUser.setGender(user.getGender());

		String password = user.getPassword();
		// 去掉密码的两端空格
		password = password.trim();

		// 用户修改火密码
		if (!(password.length() == 32)) {
			// 密码加密
			password = MD5Utils.md5(password);
			hxUser.setPassword(password);

		}

		hxUser.setPosition(user.getPosition());
		hxUser.setRealname(user.getRealname());
		hxUser.setRemark(user.getRemark());
		hxUser.setTelephone(user.getTelephone());

		// 更新日期
		hxUser.setUpdatetime(new Date());
		if (!"admin".equals(hxUser.getUsername())) {
			hxUser.setUsername(user.getUsername());
		}

		// 提交更新数据
		userMapper.updateByPrimaryKey(hxUser);

		// 删除外键关系
		HxUserRoleExample example = new HxUserRoleExample();
		example.createCriteria().andUserIdEqualTo(id);
		userRoleMapper.deleteByExample(example);

		if (StringUtils.isNoneBlank(roleIds)) {

			// 维护外键关系
			for (String roleId : roleIds) {
				HxUserRoleKey userRoleKey = new HxUserRoleKey();
				userRoleKey.setRoleId(roleId);
				userRoleKey.setUserId(id);
				userRoleMapper.insert(userRoleKey);
			}

		}
	}

	@Override
	public void deleteUserById(String id) {
		// 删除外键关系
		HxUserRoleExample example = new HxUserRoleExample();
		example.createCriteria().andUserIdEqualTo(id);
		userRoleMapper.deleteByExample(example);

		// 删除用户信息
		userMapper.deleteByPrimaryKey(id);

	}

	@Override
	public void lockUserById(String id) {
		HxUser user = userMapper.selectByPrimaryKey(id);
		// 更改状态位
		if (!"admin".equals(user.getUsername())) {
			user.setStatus(0);
			userMapper.updateByPrimaryKey(user);
		}
	}

	@Override
	public void unlockUserById(String id) {
		HxUser user = userMapper.selectByPrimaryKey(id);
		// 更改状态位
		user.setStatus(1);
		userMapper.updateByPrimaryKey(user);

	}

}
